	
		package com.cx.bank.manager;

		import java.util.Random;

import com.cx.bank.Dao.Impllog_res;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.InvalidDepositException;
import com.cx.bank.util.MyException;
	
	/**
	 * 实现用户需求的各个方法
	 * @author dengqin
	 * @version 2017/6/14
	 */
	
	public class ImplManager  implements Manager{
	
		private  static ImplManager  instance;// 单例对象
		private ImplManager(){}
		public  UserBean user = new UserBean();
		MoneyBean bean = MoneyBean.getInstance();
		public Impllog_res implreg= Impllog_res.getInstance();
		
		/**
		 *  单例
		 * @return instance
		 */
		
		public static ImplManager getInstance(){
			if(instance ==null){
				instance = new ImplManager();
			}
			return instance;
		}
		
		/**
		 * 封装登陆的表单数据，将数据传入Dao中
		 * @param name
		 * @param pass
		 * @return flag
		 */
		
		@Override
		public boolean Login(String name, String pass) throws Exception {
			// TODO Auto-generated method stub
			boolean flag =  false;
				if("".equals(name)||"".equals(pass))  throw new MyException("用户名或者密码为空！请重新输入！");
				user.setPassword(pass);
				user.setUsername(name);
				if(implreg.login(user)==false){
					flag= false;
				}
				flag = true;
			return flag;
		}

		/**
		 * 封装注册的表单数据，将数据传入Dao中
		 * @param name
		 * @param pass
		 * @return flag
		 */

		public boolean Register(String name, String pass) throws Exception {
			boolean flag =  false;
			int id =getRandomNumber();
			if("".equals(name)||"".equals(pass))  throw new MyException("输入的用户名或者密码为空！请重新输入！");
			user.setPassword(pass);
			user.setUsername(name);
			bean.setId(id);
			if(implreg.register(user,bean)==false){
				flag= false;
			}
			flag = true;
		return flag;
		}

		
		/**
		 * 存款
		 * @param s
		 * @return s+leave
		 */

		public double deposit(double money) throws Exception {
		
			//MoneyBean bean = MoneyBean.getInstance();
			double leave  = bean.getMoney();
			if(money<0) throw new InvalidDepositException("存款不能负数！请重新输入存款金额！");
			bean.setMoney(leave+money);
		//	implreg.addMoney(bean);
			return leave+money;
		}
	
		/**
		 * 取款(non-Javadoc)
		 * @see com.cx.bank.manager.Manager#withdrawals(double)
		 * @param m
		 * @return m+leave
		 */
		
		@Override
		public double withdrawals(double money) throws Exception {
			
			
			double leave =bean.getMoney();
			if(money>leave) throw new InvalidDepositException("当前余额不足！请重新输入取款金额！");
			bean.setMoney(leave-money);
			return leave-money;
		}
		
		/**
		 * 查询用户余额
		 */
		
		@Override
		public double inquery() throws Exception {
		
		MoneyBean bean = MoneyBean.getInstance();
			double leave = bean.getMoney();
			return leave;
		}
		
		/**
		 * 用户转账处理
		 * @param name
		 *  @param money
		 *  @param id
		 *  @return false/true
		 */

		@Override
		public boolean transfer(String name, int id,double money) throws Exception {
			// TODO Auto-generated method stub
		MoneyBean mon =MoneyBean.getInstance();
			if(money<0) throw new InvalidDepositException("转账金额为负！请重新输入！");
			double t_money = withdrawals(money);
		System.out.println("余额为："+t_money);
			bean.setMoney(t_money);
			mon.setId(id);
			if(implreg.transfer(money, name)==false){
				return false;
			}
			return true;
		}
		
		/**
		 * 生成随机卡号（账号）后三位
		 * @return temp
		 */
		
		private int getRandomNumber(){
				int temp = 0;
				int min = (int) Math.pow(10, 2);
				int max = (int) Math.pow(10, 3);
				Random rand = new Random();
				while(true){
					temp = rand.nextInt(max);
					if(temp >= min)
						break;
				}
				return temp;
		}

		/**
		 * 退出银行系统
		 */
	
		public void exit() throws Exception {
			double	money=bean.getMoney();
				implreg.addMoney(money,user);
			System.out.println("成功退出银行系统1.0");
			  System.exit(0);//关闭当前进程。
		
			
		}

		
		
	}
